<?php

require_once("CMySQL.php");

class CCollection
{
	protected $baseUrl;
	private $docs;
	
	public function __construct($config)
	{
		$this->baseUrl=$config["baseUrl"];
		$this->docs=array();
	}
	
	public function add($obj)
	{
		$this->docs=array_merge($this->docs, $obj->getDocs());
	}
	
	public function showDocument($key)
	{
		$c = curl_init();
		$url="{$this->baseUrl}/execute?{$this->docs[$key]["query"]}";
		curl_setopt($c, CURLOPT_URL, $url);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($c, CURLOPT_HEADER, false);
		curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0); 
		$output = curl_exec($c);
		if($output === false)
		{
			trigger_error('Erreur curl : '.curl_error($c), E_USER_WARNING);
		}else
		{
			header("Content-type: application/pdf");
			header("Content-Length: ".strlen($output));
			header("Content-Disposition: attachment; filename=\"{$this->docs[$key]["docName"]}.pdf\"");
			echo $output;
		}
		curl_close($c);
	}
};

abstract class CDocuments
{
	protected $docs;
	protected $mysql;
	protected $table;
	protected $dbName;
	
	public function __construct($config, $dossier) 
	{
		$this->mysql=CMySQL::getInstance();
		$this->dbName=$this->mysql->getDbNameFromDossier($dossier);
		$this->mysql->selectDb($this->dbName);
		
		$this->table="";
		$this->makeTableHeader();
		$this->make($this->getSql());
		$this->makeTableFooter();
	}
	
	private function make($sql)
	{
		$result=$this->mysql->execQuery($sql);
		while($row=mysql_fetch_array($result))
		{	
			$query=$this->getQuery($row);
			$key=md5($query);
	
			$this->docs[$key]["docName"]=$row["docName"];
			$this->docs[$key]["query"]=$query;
			
			$this->makeTableLigne($row);
		}
	}
	
	public function getTable() 
	{
		return $this->table;
	}
	
	public function getDocs() 
	{
		return $this->docs;
	}
	
	abstract protected function getSql();
	
	abstract protected function getQuery($row);
	
	abstract protected function makeTableHeader();
	
	abstract protected function makeTableFooter();
	
	abstract protected function makeTableLigne($row);
};

class CFactures extends CDocuments
{
	protected function getSql()
	{
		$sql="SELECT code, nomClient AS nom, DATE_FORMAT(creation, \"%d/%m/%Y\") AS creation, ";
		$sql.="CONCAT(document, DATE_FORMAT(creation, \"%m%y\"), \"AR\", code) AS docName, ";
		$sql.="FORMAT(SUM(prixUnitaireHT*quantite*IF(document=\"A\", -1, 1)), 2) AS montant ";
		$sql.="FROM teteFactures INNER JOIN ligneFactures ON facture=code ";
		$sql.="GROUP BY code";
		
		return $sql;
	}
	
	protected function getQuery($row)
	{
		 return "report=facture&render=PDF&fromNumeroFacture={$row["code"]}&toNumeroFacture={$row["code"]}&docName={$row["docName"]}&dossier={$this->dbName}";
	}	
	
	protected function makeTableHeader()
	{
		$this->table.="<h1 align='center'>Liste des factures</h1>";
		$this->table.="<table border='1' cellspacing='0' cellpadding='0' width='50%' align='center'>";
		$this->table.="<tr>";
		$this->table.="<th width='10%'>Numéro</th>";
		$this->table.="<th width='15%'>Du</th>";
		$this->table.="<th>Nom du client</th>";
		$this->table.="<th width='15%'>Montant</th>";
		$this->table.="</tr>";
	}
	
	protected function makeTableFooter()
	{
		$this->table."</table>";
	}
	
	protected function makeTableLigne($row)
	{
		$this->table.="<tr>";
		$this->table.="<td align='right'>";
		$this->table.="<a href='showDocument.php?ref=".md5($this->getQuery($row))."'>{$row["code"]}</a>";
		$this->table.="</td>";
		$this->table.="<td>{$row["creation"]}</td>	";
		$this->table.="<td>".utf8_encode($row["nom"])."</td>	";
		$this->table.="<td align='right'>{$row["montant"]}</td>";
		$this->table.="</tr>";
	}
};

class CDevis extends CDocuments
{
	protected function getSql()
	{
		$sql="SELECT td.code, IFNULL(p.nom, c.nom) as nom, ";
		$sql.="CONCAT(\"D\", DATE_FORMAT(td.creation, \"%m%y\"), \"AR\", td.code) AS docName ";
		$sql.="FROM teteDevis AS td LEFT JOIN prospect AS p ON codeProspect=p.code ";
		$sql.="LEFT JOIN client AS c ON codeClient=c.code ";
		$sql.="ORDER BY td.code";
		
		return $sql;
	}
	
	protected function getQuery($row)
	{
		return "report=devis&render=PDF&numeroDevis={$row["code"]}&docName={$row["docName"]}&dossier={$this->dbName}";
	}	
	
	protected function makeTableHeader()
	{
		$this->table.="<h1 align='center'>Liste des devis</h1>";
		$this->table.="<table border='1' cellspacing='0' cellpadding='0' width='50%' align='center'>";
		$this->table.="<tr>";
		$this->table.="<th width='20%'>Numéro</th>";
		$this->table.="<th>Nom du client / prospect</th>";
		$this->table.="</tr>";
	}
	
	protected function makeTableFooter()
	{
		$this->table."</table>";
	}
	
	protected function makeTableLigne($row)
	{
		$this->table.="<tr>";
		$this->table.="<td align='right'>";
		$this->table.="<a href='showDocument.php?ref=".md5($this->getQuery($row))."'>{$row["code"]}</a>";
		$this->table.="</td>";
		$this->table.="<td>".utf8_encode($row["nom"])."</td>	";
		$this->table.="</tr>";
	}
};
?>
